package com.sls.game.service.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.core.util.DataConversion;
import com.sls.core.util.ObjectIncreaseId;
import com.sls.core.util.ServerDateAndTime;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.GoodsInfoManageDAO;
import com.sls.game.dao.GoodsSupplyManageDAO;
import com.sls.game.dao.NutsNetGoodsInfoDAO;
import com.sls.game.dao.OhFeiGoodsInfoDAO;
import com.sls.game.dao.PriceTemplateDAO;
import com.sls.game.dao.ProductBrandDAO;
import com.sls.game.dao.SpecialPriceSystemDAO;
import com.sls.game.dao.TencentGoodsInfoDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.GoodsManageInfo;
import com.sls.game.pojo.GoodsSupplyManageInfo;
import com.sls.game.pojo.NutsNetGoodsInfo;
import com.sls.game.pojo.OhFeiGoodsInfo;
import com.sls.game.pojo.ProductBrandInfo;
import com.sls.game.pojo.SpecialPriceSystemInfo;
import com.sls.game.pojo.TencentGoodsInfo;
import com.sls.game.service.GoodsInfoManageService;

/**
 * 
 * @description 商品信息管理
 * @author Wangxuegang
 * @date 2012-9-14
 * 
 */
@Service("goodsInfoManageService")
public class GoodsInfoManageServiceImpl implements GoodsInfoManageService {

	private static Logger log = Logger
			.getLogger(GoodsInfoManageServiceImpl.class);

	private Gson gson = null;

	public GoodsInfoManageServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 商品信息表DAO
	 */
	private GoodsInfoManageDAO goodsInfoManageDAO;

	/**
	 * 商品供应表DAO
	 */
	private GoodsSupplyManageDAO goodsSupplyManageDAO;

	/**
	 * 坚果商品信息DAO
	 */
	private NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO;

	/**
	 * 殴飞商品信息DAO
	 */
	private OhFeiGoodsInfoDAO ohFeiGoodsInfoDAO;

	/**
	 * 腾讯商品信息DAO
	 */
	private TencentGoodsInfoDAO tencentGoodsInfoDAO;

	/**
	 * 品牌表DAO
	 */
	private ProductBrandDAO productBrandDAO;

	/**
	 * 模版表DAO
	 */
	private PriceTemplateDAO priceTemplateDAO;
	@Resource(name = "specialPriceSystemDAO")
	private SpecialPriceSystemDAO priceSystemDAO;

	public GoodsSupplyManageDAO getGoodsSupplyManageDAO() {
		return goodsSupplyManageDAO;
	}

	@Resource(name = "goodsSupplyManageDAO")
	public void setGoodsSupplyManageDAO(
			GoodsSupplyManageDAO goodsSupplyManageDAO) {
		this.goodsSupplyManageDAO = goodsSupplyManageDAO;
	}

	public PriceTemplateDAO getPriceTemplateDAO() {
		return priceTemplateDAO;
	}

	@Resource(name = "priceTemplateDAO")
	public void setPriceTemplateDAO(PriceTemplateDAO priceTemplateDAO) {
		this.priceTemplateDAO = priceTemplateDAO;
	}

	public ProductBrandDAO getProductBrandDAO() {
		return productBrandDAO;
	}

	@Resource(name = "productBrandDAO")
	public void setProductBrandDAO(ProductBrandDAO productBrandDAO) {
		this.productBrandDAO = productBrandDAO;
	}

	public TencentGoodsInfoDAO getTencentGoodsInfoDAO() {
		return tencentGoodsInfoDAO;
	}

	@Resource(name = "tencentGoodsInfoDAO")
	public void setTencentGoodsInfoDAO(TencentGoodsInfoDAO tencentGoodsInfoDAO) {
		this.tencentGoodsInfoDAO = tencentGoodsInfoDAO;
	}

	public OhFeiGoodsInfoDAO getOhFeiGoodsInfoDAO() {
		return ohFeiGoodsInfoDAO;
	}

	@Resource(name = "ohFeiGoodsInfoDAO")
	public void setOhFeiGoodsInfoDAO(OhFeiGoodsInfoDAO ohFeiGoodsInfoDAO) {
		this.ohFeiGoodsInfoDAO = ohFeiGoodsInfoDAO;
	}

	public NutsNetGoodsInfoDAO getNutsNetGoodsInfoDAO() {
		return nutsNetGoodsInfoDAO;
	}

	@Resource(name = "nutsNetGoodsInfoDAO")
	public void setNutsNetGoodsInfoDAO(NutsNetGoodsInfoDAO nutsNetGoodsInfoDAO) {
		this.nutsNetGoodsInfoDAO = nutsNetGoodsInfoDAO;
	}

	public GoodsInfoManageDAO getGoodsInfoManageDAO() {
		return goodsInfoManageDAO;
	}

	@Resource(name = "goodsInfoManageDAO")
	public void setGoodsInfoManageDAO(GoodsInfoManageDAO goodsInfoManageDAO) {
		this.goodsInfoManageDAO = goodsInfoManageDAO;
	}

	/**
	 * 商品信息分页查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryPageInquire(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsManageInfo goodsManageInfo = gson.fromJson(body,
				GoodsManageInfo.class);
		boolean flag = checkDataOfQueryPageInquire(goodsManageInfo,
				head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				String resultBody = null;
				Pagination<GoodsManageInfo> paginationList = null;
				paginationList = goodsInfoManageDAO
						.queryPaginationList(goodsManageInfo);

				if (paginationList != null) {
					resultBody = gson.toJson(paginationList);
				}

				log.info("分页查询结果为：  " + resultBody);
				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return resultBody;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！",e);
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 商品信息单条记录查询
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryByGoodsId(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsManageInfo goodsManageInfo = gson.fromJson(body,
				GoodsManageInfo.class);
		boolean flag = checkDataOfqueryByGoodsId(goodsManageInfo,
				head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				String resultBody = null;
				String goodsId = goodsManageInfo.getGoodsId();
				GoodsManageInfo goodsManageInfo1 = null;
				goodsManageInfo1 = goodsInfoManageDAO
						.queryGoodsInfoByGoodsId(goodsId);

				if (goodsManageInfo1 != null) {
					resultBody = gson.toJson(goodsManageInfo1);
				}

				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return resultBody;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 商品新增
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String insert(String body, Head head) throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsManageInfo goodsManageInfo = gson.fromJson(body,
				GoodsManageInfo.class);
		boolean flag = checkDataOfInsert(goodsManageInfo, head.getRequestId());
		if (flag) { // 数据校验通过
			// 商品名称
			String goodsName = "";

			String maxGoodsId = null;
			String goodsId = "";
			maxGoodsId = goodsInfoManageDAO.queryMaxGoodsId();
			if ("".equals(maxGoodsId) || maxGoodsId == null) {
				goodsId = "100000000";
			} else {
				goodsId = ObjectIncreaseId.increaseObjectId(maxGoodsId);
			}

			GoodsManageInfo goodsManageInfo1 = new GoodsManageInfo();
			goodsManageInfo1.setGoodsId(goodsId);

			goodsName = goodsManageInfo.getGoodsName();
			// 商品插入之前，判断该条商品是否存在
			boolean isExist = isExistOfGoods(goodsName);
			if (isExist) {
				goodsManageInfo1.setId(UUID.randomUUID().toString());
				goodsManageInfo1.setGoodsName(goodsName);
				goodsManageInfo1.setRechargeMode(goodsManageInfo
						.getRechargeMode());
				goodsManageInfo1.setGoodsCategoryId(goodsManageInfo
						.getGoodsCategoryId());
				goodsManageInfo1.setCommonSalePrice(goodsManageInfo
						.getCommonSalePrice());
				goodsManageInfo1.setGoodsMoney(goodsManageInfo.getGoodsMoney());
				goodsManageInfo1.setGoodsState("1"); // 0:商品下架 1：商品上架
				goodsManageInfo1.setGoodsDescription(goodsManageInfo
						.getGoodsDescription());
				goodsManageInfo1.setEnable(goodsManageInfo.getEnable()); // 0：不启用
																			// 1:启用
				goodsManageInfo1.setRechargeArea(goodsManageInfo
						.getRechargeArea());
				goodsManageInfo1.setStateMent(goodsManageInfo.getStateMent());
				goodsManageInfo1.setRemark(goodsManageInfo.getRemark());
				goodsManageInfo1.setOperator(goodsManageInfo.getOperator());
				goodsManageInfo1.setHeat(goodsManageInfo.getHeat());
				
				// 根据品牌Id，查询品牌名称
				String brandId = goodsManageInfo.getGoodsCategoryId();
				ProductBrandInfo productBrandInfo = null;
				productBrandInfo = productBrandDAO.queryById(brandId);
				// 品牌名称
				String brandName = "";
				if (productBrandInfo != null) {
					brandName = productBrandInfo.getGameName();
				} else {
					throw new ServerDisponseException("NO_BRAND", "无此品牌！");
				}
				goodsManageInfo1.setBrandName(brandName);
				// 记录插入时间
				String now = ServerDateAndTime.getNowDateTimeOfFormat();
				goodsManageInfo1.setInsertTime(now);

				try {
					goodsInfoManageDAO.insert(goodsManageInfo1);
				} catch (Exception e) {
					log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！",
							e);
					throw new ServerDisponseException(
							ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
							ExceptionCodeDes.ACCESS_DATABASE_ERROR
									.getDescription());
				}

				String resultBody = gson.toJson(goodsManageInfo1);
				return resultBody;
			} else {
				throw new ServerDisponseException(
						ExceptionCodeDes.GOODS_EXIST.getCode(),
						ExceptionCodeDes.GOODS_EXIST.getDescription());
			}
		}

		return null;
	}

	/**
	 * 新增商品为腾讯产品
	 * 
	 * @param originalGoodsId
	 * @param goodsName
	 * @param goodsManageInfo1
	 * @param head
	 * @return
	 */
	@SuppressWarnings("unused")
	private GoodsManageInfo addGoodsOfTencent(String originalGoodsId,
			GoodsManageInfo goodsManageInfo1, Head head) {
		TencentGoodsInfo info = null;
		String goodsName = "";
		info = tencentGoodsInfoDAO.queryGoodsInfoGoodsId(originalGoodsId);
		if (info != null) {
			// 商品名称
			goodsName = info.getProductName();
			// 充值方式
			String rechargeMode = info.getRechargeMode();
			goodsManageInfo1.setGoodsName(goodsName);
			goodsManageInfo1.setRechargeMode(rechargeMode);
			goodsManageInfo1.setGoodsName(goodsName);
		} else {
			log.error("Service处理[" + head.getRequestId() + "]的请求，原始商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}

		return goodsManageInfo1;
	}

	/**
	 * 新增商品为坚果网络商品
	 * 
	 * @param originalGoodsId
	 * @param goodsName
	 * @param goodsManageInfo1
	 * @param head
	 * @return
	 */
	@SuppressWarnings("unused")
	private GoodsManageInfo addGoodsOfNutsNet(String originalGoodsId,
			GoodsManageInfo goodsManageInfo1, Head head) {
		NutsNetGoodsInfo nutsNetGoodsInfo = null;
		String goodsName = "";
		// 根据原始商品Id，查询单条记录
		nutsNetGoodsInfo = queryNutsnetOriGoods(originalGoodsId);
		if (nutsNetGoodsInfo != null) {
			String name = nutsNetGoodsInfo.getName();
			String[] strArr = new String[3];
			strArr = name.split("-");
			// 商品名称
			goodsName = strArr[1] + strArr[2];
			String parValueStr = nutsNetGoodsInfo.getParvalue();
			int index = -1;
			index = parValueStr.indexOf(".");
			if (index != -1) {
				parValueStr = parValueStr.substring(0, index);
			}

			// 商品面值
			int parValue = Integer.parseInt(parValueStr) * 1000; // 单位：厘
			// 官方网址
			String officialUrl = "";
			// 充值方式
			String rechargeMode = nutsNetGoodsInfo.getProductType();

			goodsManageInfo1.setGoodsMoney(parValue);
			goodsManageInfo1.setOfficialUrl(officialUrl);
			goodsManageInfo1.setRechargeMode(rechargeMode);
			goodsManageInfo1.setGoodsName(goodsName);
		} else {
			log.error("Service处理[" + head.getRequestId() + "]的请求，原始商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}

		return goodsManageInfo1;
	}

	/**
	 * 新增商品为殴飞商品
	 * 
	 * @param originalGoodsId
	 * @param goodsName
	 * @param goodsManageInfo1
	 * @param head
	 * @return
	 */
	@SuppressWarnings("unused")
	private GoodsManageInfo addGoodsOfOhFei(String originalGoodsId,
			GoodsManageInfo goodsManageInfo1, Head head) {
		OhFeiGoodsInfo ohFeiGoodsInfo = null;
		String goodsName = "";
		ohFeiGoodsInfo = ohFeiGoodsInfoDAO.queryById(originalGoodsId);
		if (ohFeiGoodsInfo != null) {
			// 商品名称
			goodsName = ohFeiGoodsInfo.getGoodsName();
			// 商品面值
			String parValueOri = ohFeiGoodsInfo.getParValue();
			int parValue = DataConversion.toInt(parValueOri); // 单位转换为厘
			// 官方网址
			String officialUrl = ohFeiGoodsInfo.getFullCostsite();
			// 充值方式
			String rechargeMode = ohFeiGoodsInfo.getClassType();

			goodsManageInfo1.setGoodsMoney(parValue);
			goodsManageInfo1.setOfficialUrl(officialUrl);
			if ("3".equals(rechargeMode)) {
				goodsManageInfo1.setRechargeMode("1");
			} else {
				goodsManageInfo1.setRechargeMode("2");
			}

			goodsManageInfo1.setGoodsName(goodsName);
		} else {
			log.error("Service处理[" + head.getRequestId() + "]的请求，原始商品不存在！");
			throw new ServerDisponseException(
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getCode(),
					ExceptionCodeDes.ORIGINAL_GOODS_NOT_EXTIS.getDescription());
		}

		return goodsManageInfo1;
	}

	/**
	 * 商品插入之前，判断该条商品是否存在
	 * 
	 * @param goodsName
	 * @param supplyId
	 * @return
	 */
	private boolean isExistOfGoods(String goodsName) {
		boolean flag = false;
		GoodsManageInfo info = null;
		info = goodsInfoManageDAO.queryByGoodsName(goodsName);
		if (info == null) {
			flag = true;
		}

		return flag;
	}

	/**
	 * 查询坚果网络原始产品
	 * 
	 * @param originalGoodsId
	 */
	private NutsNetGoodsInfo queryNutsnetOriGoods(String originalGoodsId) {
		NutsNetGoodsInfo info = null;
		info = nutsNetGoodsInfoDAO.queryById(originalGoodsId);

		return info;
	}

	/**
	 * 商品更新（修改）
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String update(String body, Head head) throws ServerDisponseException {
		// 对消息body进行解析
		GoodsManageInfo goodsManageInfo = gson.fromJson(body,
				GoodsManageInfo.class);
		boolean flag = checkDataOfupdate(goodsManageInfo, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				String goodsId = goodsManageInfo.getGoodsId();
				String goodsName = goodsManageInfo.getGoodsName();
				String operator = goodsManageInfo.getOperator();
				String oldGoodsName = goodsInfoManageDAO
						.queryGoodsNameByGoodsId(goodsId);
				// 记录更新时间
				String now = ServerDateAndTime.getNowDateTimeOfFormat();
				goodsManageInfo.setModifyTime(now);
				goodsInfoManageDAO.update(goodsManageInfo);

				if (!oldGoodsName.equals(goodsName)) {
					GoodsSupplyManageInfo supply = new GoodsSupplyManageInfo();
					supply.setGoodsId(goodsId);
					supply.setGoodsName(goodsName);
					supply.setOperator(operator);
					supply.setModifyTime(now);
					this.goodsSupplyManageDAO.updateByGoodsId(supply);

					SpecialPriceSystemInfo systemInfo = new SpecialPriceSystemInfo();
					systemInfo.setGoodsId(goodsId);
					systemInfo.setGoodsName(goodsName);
					systemInfo.setOperator(operator);
					systemInfo.setModifyTime(now);
					this.priceSystemDAO.updateByGoodsId(systemInfo);
				}

				return null;
			} catch (Exception e) {
				log.error("更新异常", e);
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 商品上架、下架操作
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String updateUpOrDownFrame(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsManageInfo goodsManageInfo = gson.fromJson(body,
				GoodsManageInfo.class);
		boolean flag = checkDataOfDelete(goodsManageInfo, head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				// 更新记录的时间
				String now = ServerDateAndTime.getNowDateTimeOfFormat();
				goodsManageInfo.setModifyTime(now);
				goodsInfoManageDAO.updateState(goodsManageInfo);

				// 如果是下架 ，供应表所有该商品都下架
				String goodsState = goodsManageInfo.getGoodsState();
				if ("0".equals(goodsState)) { // 下架
					pullDownShelf(goodsManageInfo);
				}

				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return null;
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}

		}

		return null;
	}

	/**
	 * 如果是下架 ，供应表所有该商品都下架
	 * 
	 * @param goodsManageInfo
	 */
	private void pullDownShelf(GoodsManageInfo goodsManageInfo) {
		String goodsId = goodsManageInfo.getGoodsId();
		GoodsSupplyManageInfo suppInfo = new GoodsSupplyManageInfo();
		suppInfo.setGoodsId(goodsId);
		suppInfo.setGoodsState("1"); // 上架
		List<GoodsSupplyManageInfo> list = null;
		list = goodsSupplyManageDAO.queryByGoodsId(suppInfo);
		if (list != null && list.size() > 0) {
			for (GoodsSupplyManageInfo goSuInfo : list) {
				String id = goSuInfo.getId();
				suppInfo.setId(id);
				suppInfo.setGoodsState("0"); // 下架
				try {
					goodsSupplyManageDAO.updateState(suppInfo);
				} catch (Exception e) {
					log.error(e);
				}
			}
		}
	}

	/**
	 * 根据品牌Id，查询该品牌下的所有商品
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 * @date 2012-09-25
	 */
	@Override
	public String queryListByBrandId(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsManageInfo goodsManageInfo = gson.fromJson(body,
				GoodsManageInfo.class);
		boolean flag = checkDataOfQueryListByBrandId(goodsManageInfo,
				head.getRequestId());
		if (flag) { // 数据校验通过
			try {
				String brandId = goodsManageInfo.getGoodsCategoryId();
				List<GoodsManageInfo> list = null;
				GoodsManageInfo goodsManageInfo1 = new GoodsManageInfo();
				goodsManageInfo1.setGoodsCategoryId(brandId);
				goodsManageInfo1.setGoodsState(goodsManageInfo.getGoodsState()); // 上架的商品
				list = goodsInfoManageDAO.queryListByBrandId(goodsManageInfo1);
				String resultBody = null;

				if (list != null) {
					resultBody = gson.toJson(list);
				}

				if (log.isInfoEnabled()) {
					log.info("Service处理[" + head.getRequestId() + "]的请求已结束");
				}
				return resultBody;

			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}
		}

		return null;
	}

	/**
	 * 查询未加入到某一价格模版下的 所有 商品名称
	 * 
	 * @param body
	 * @param head
	 * @return
	 * @throws ServerDisponseException
	 */
	@Override
	public String queryNotInSpecialPrice(String body, Head head)
			throws ServerDisponseException {
		if (log.isInfoEnabled()) {
			log.info("进入Service开始处理[" + head.getRequestId() + "]的请求");
		}
		// 对消息body进行解析
		GoodsManageInfo goodsManageInfo = gson.fromJson(body,
				GoodsManageInfo.class);
		boolean flag = checkDataOfQueryNotInSpecialPrice(goodsManageInfo,
				head.getRequestId());
		if (flag) { // 数据校验通过
			List<GoodsManageInfo> list = null;
			// 模版名称
			String templateName = goodsManageInfo.getTemplateName();
			// 模版编号
			String templateCode = null;

			try {
				templateCode = priceTemplateDAO
						.queryTemplateCodeByName(templateName);

				GoodsManageInfo info = new GoodsManageInfo();
				info.setTemplateCode(templateCode);
				list = goodsInfoManageDAO
						.queryNotInSpecialPriceOFTemPlate(info);
			} catch (Exception e) {
				log.error("Service处理[" + head.getRequestId() + "]的请求出现异常！");
				throw new ServerDisponseException(
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
						ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
			}

			String resultBody = null;
			if (list != null) {
				resultBody = gson.toJson(list);
			}

			return resultBody;
		}

		return null;
	}

	/**
	 * /** 查询未加入到供货规则下的所有商品编号和名称
	 * 
	 * @return
	 */
	@Override
	public String queryGoodsIdAndNameNotInRule(Head head)
			throws ServerDisponseException {
		List<GoodsManageInfo> list = null;
		try {
			list = this.goodsInfoManageDAO.queryGoodsIdAndNameNotInRule();
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		if (list != null) {
			return gson.toJson(list);
		}

		return null;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfQueryNotInSpecialPrice(
			GoodsManageInfo goodsManageInfo, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		// 模板名称
		String templateName = goodsManageInfo.getTemplateName();
		if ("".equals(templateName) || templateName == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.TEMPLATE_NAME_NULL.getCode(),
					ExceptionCodeDes.TEMPLATE_NAME_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfQueryPageInquire(
			GoodsManageInfo goodsManageInfo, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		String currentPage = goodsManageInfo.getCurrentPage() + "";
		String pageSize = goodsManageInfo.getPageSize() + "";
		if ("".equals(currentPage) || currentPage == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else if ("".equals(pageSize) || pageSize == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfqueryByGoodsId(GoodsManageInfo goodsManageInfo,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		String goodsId = goodsManageInfo.getGoodsId();
		if ("".equals(goodsId) || goodsId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_ID_EMPTY.getCode(),
					ExceptionCodeDes.GOODS_ID_EMPTY.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfInsert(GoodsManageInfo goodsManageInfo,
			String reqId) throws ServerDisponseException {
		boolean flag = false;

		// 商品品牌编号
		String goodsCategoryId = goodsManageInfo.getGoodsCategoryId();
		// 默认售价
		String commonSalePrice = goodsManageInfo.getCommonSalePrice() + "";
		// 操作员账号
		String operator = goodsManageInfo.getOperator();

		if ("".equals(goodsCategoryId) || goodsCategoryId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.BRAND_ID_NULL.getCode(),
					ExceptionCodeDes.BRAND_ID_NULL.getDescription());
		} else if ("".equals(commonSalePrice) || commonSalePrice == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.DEFAULT_PRICE_NULL.getCode(),
					ExceptionCodeDes.DEFAULT_PRICE_NULL.getDescription());
		} else if ("".equals(operator) || operator == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.OPERATOR_NO_NULL.getCode(),
					ExceptionCodeDes.OPERATOR_NO_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfupdate(GoodsManageInfo goodsManageInfo,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		// 商品编号
		String goodsId = goodsManageInfo.getGoodsId();

		GoodsManageInfo goodsManageInfo1 = goodsInfoManageDAO
				.queryGoodsInfoByGoodsId(goodsId);
		String goodsId1 = goodsManageInfo1.getGoodsId();

		if (!(goodsId.equals(goodsId1))) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODSID_NOT_AGREE.getCode(),
					ExceptionCodeDes.GOODSID_NOT_AGREE.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfDelete(GoodsManageInfo goodsManageInfo,
			String reqId) throws ServerDisponseException {
		boolean flag = false;
		// 编号
		String id = goodsManageInfo.getId();
		// 商品状态
		String goodsState = goodsManageInfo.getGoodsState();
		if (StringUtils.isEmpty(id)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		} else if ("".equals(goodsState) || goodsState == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.GOODS_STATUS_NULL.getCode(),
					ExceptionCodeDes.GOODS_STATUS_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	/**
	 * 数据校验
	 */
	private boolean checkDataOfQueryListByBrandId(
			GoodsManageInfo goodsManageInfo, String reqId)
			throws ServerDisponseException {
		boolean flag = false;
		// 品牌编号
		String brandId = goodsManageInfo.getGoodsCategoryId();
		if ("".equals(brandId) || brandId == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.BRAND_ID_NULL.getCode(),
					ExceptionCodeDes.BRAND_ID_NULL.getDescription());
		} else {
			flag = true;
		}

		return flag;
	}

	@Override
	public void deleteByGoodsId(String body, Head head) {
		GoodsManageInfo info = gson.fromJson(body, GoodsManageInfo.class);
		String goodsId = "";
		if (info != null) {
			goodsId = info.getGoodsId();
		}
		try {
			this.goodsInfoManageDAO.deleteByGoodsId(goodsId);
		} catch (Exception e) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
	}

}
